 
const initstate = {
  cartlist: wx.getStorageSync('cartlist')||[]
  // amount: wx.getStorageSync('cartlist')||0
}
export default (state = initstate, action) => {
  switch (action.type) {
    case 'addtocart':{
      const has = state.cartlist.some(item => item.id === action.payload.id)
      if(has){
        state.cartlist = state.cartlist.map(item=>{
          if (item.id === action.payload.id) {  
            item.count = item.count + 1}    
          return item
        })
      }
      else {state.cartlist.push(action.payload)}
      wx.setStorageSync('cartlist', state.cartlist)
      return state  
    }
    case 'add':{
      state.cartlist = state.cartlist.map(item => {
        if (item.id === action.payload) {
          item.count = item.count + 1
        }
        return item
      })
      wx.setStorageSync('cartlist', state.cartlist)
      return state
    }
    case 'reduce': {
      state.cartlist = state.cartlist.map(item => {
        if (item.id === action.payload) {
          item.count = item.count - 1
        }
        return item
      })
      state.cartlist = state.cartlist.filter(item => item.count !== 0)
      wx.setStorageSync('cartlist', state.cartlist)
      return state
    }
    case 'remove': {
      state.cartlist = state.cartlist.filter(item => {
        return item.id !== action.payload
      })
      wx.setStorageSync('cartlist', state.cartlist)
      return state
    }
    case 'clear': {
      state.cartlist = []
      wx.setStorageSync('cartlist', state.cartlist)
      return state
    }   
    default:
      return state
      wx.setStorageSync('cartlist', state.cartlist)
  }
  }
